package cn.dev33.satoken.oauth2.model;

import com.csap.framework.annotation.apidoc.ApiModel;
import com.csap.framework.annotation.apidoc.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Model: Access-Token
 *
 * @author kong
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Accessors(chain = true)
@ApiModel(value = "Access-Token info")
public class AccessTokenModel implements Serializable {

    private static final long serialVersionUID = -6541180061782004705L;
    /**
     * userToken 值
     */
    @ApiModelProperty(value = "userToken", forceRep = true, required = true)
    private String userToken;
    /**
     * 用户登录类型
     */
    @ApiModelProperty(value = "loginType", forceRep = true, required = true)
    private String loginType;
    /**
     * Access-Token 值
     */
    @ApiModelProperty(value = "accessToken", forceRep = true, required = true)
    private String accessToken;

    /**
     * Refresh-Token 值
     */
    @ApiModelProperty(value = "refreshToken", forceRep = true, required = true)
    private String refreshToken;

    /**
     * Access-Token 到期时间
     */
    @ApiModelProperty(value = "Access-Token到期时间", forceRep = true, required = true)
    private Long expiresTime;

    /**
     * Refresh-Token 到期时间
     */
    @ApiModelProperty(value = "Refresh-Token到期时间", forceRep = true, required = true)
    private Long refreshExpiresTime;
    /**
     * 到期时间
     */
    @ApiModelProperty(value = "Access-Token到期时间", forceRep = true, required = true)
    private Long expiresIn;
    /**
     * 应用id
     */
    @ApiModelProperty(value = "应用id", forceRep = true, required = true)
    private String clientId;

    /**
     * 账号id
     */
    @ApiModelProperty(value = "账号id", forceRep = true, required = true)
    private Object loginId;

    /**
     * 开放账号id
     */
    @ApiModelProperty(value = "开放账号id", forceRep = true, required = true)
    private String openId;

    /**
     * 授权范围
     */
    @ApiModelProperty(value = "授权范围", forceRep = true, required = true)
    private String scope;

    /**
     * 构建一个
     *
     * @param accessToken accessToken
     * @param clientId    应用id
     * @param scope       请求授权范围
     * @param loginId     对应的账号id
     */
    public AccessTokenModel(String accessToken, String clientId, Object loginId, String scope) {
        super();
        this.accessToken = accessToken;
        this.clientId = clientId;
        this.loginId = loginId;
        this.scope = scope;
    }

    /**
     * 获取：此 Access-Token 的剩余有效期（秒）
     *
     * @return see note
     */
    public long getExpiresIn() {
        long s = (expiresTime - System.currentTimeMillis()) / 1000;
        return s < 1 ? -2 : s;
    }

    /**
     * 获取：此 Refresh-Token 的剩余有效期（秒）
     *
     * @return see note
     */
    public long getRefreshExpiresIn() {
        if (refreshExpiresTime == null) {
            return -2;
        }
        long s = (refreshExpiresTime - System.currentTimeMillis()) / 1000;
        return s < 1 ? -2 : s;
    }

    /**
     * 将所有属性转换为下划线形式的Map
     *
     * @return 属性转Map
     */
    public Map<String, Object> toLineMap() {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("access_token", accessToken);
        map.put("refresh_token", refreshToken);
        map.put("expires_in", getExpiresIn());
        map.put("refresh_expires_in", getRefreshExpiresIn());
        map.put("client_id", clientId);
        map.put("scope", scope);
        map.put("openid", openId);
        return map;
    }

}
